﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Sripirom.Pattern.UnitTest.Model.TestCases;
using Sripirom.Pattern.UnitTest.Repository.Configuration.Helpers;

namespace Sripirom.Pattern.UnitTest.Repository.Configuration.Mapper
{
    public class UnitTestCategoryMapper : IMapper<UnitTestCategory>
    {
        public Func<System.Xml.Linq.XElement, UnitTestCategory> Selector
        {
            get
            {
                return x => new UnitTestCategory(x.Descendants("UnitTestClass").Select(UnitTestClassSelector).ToList())
                {
                    Id = x.Attribute("Id").Value,
                    Name = x.Attribute("Name").Value

                };
            }
        }

        private Func<XElement, UnitTestClass> UnitTestClassSelector
        {
            get
            {
                return x => new UnitTestClass(x.Descendants("UnitTestFunction").Select(UnitTestFunctionSelector).ToList())
                {
                    Id = x.Attribute("Id").Value,
                    Name = x.Attribute("Name").Value,
                    Type = x.Attribute("Type").Value
                };
            }
        }

        private Func<XElement, UnitTestFunction> UnitTestFunctionSelector
        {
            get
            {
                return x => new UnitTestFunction(x.Descendants("UnitTestCase").Select(UnitTestCaseSelector).ToList())
                {
                    Id = x.Attribute("Id").Value
                };
            }
        }

        private Func<XElement, UnitTestCase> UnitTestCaseSelector
        {
            get
            {
                return x => new UnitTestCase(x.Descendants("UnitTestParameter").Select(UnitTestParameterSelector).ToList())
                {
                    Id = x.Attribute("Id").Value,
                    Name = x.Attribute("Name").Value,
                    Description = x.Attribute("Description").Value,
                    IsError = x.Attribute("IsError").GetBoolean(),
                    Ignore = x.Attribute("Ignore").Value
                };
            }
        }

        protected Func<XElement, UnitTestParameter> UnitTestParameterSelector
        {
            get
            {
                return x => new UnitTestParameter()
                {
                    Id = x.Attribute("Id").Value,
                    Name = x.Attribute("Name").Value,
                    Type = x.Attribute("Type").Value,
                    Value = x.Attribute("Value").Value
                };
            }
        }
   
    }
}
